package com.example.springstudy.base.aspect;

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.SQLException;
import java.util.Set;

@Slf4j
public class DataScopeInterceptor implements InnerInterceptor {

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        String firstSql = boundSql.getSql();

        log.info("interceptor-sql:{}", firstSql);
//        StringBuilder sql = new StringBuilder();
//        if(SecurityUtils.getUser().isEmpty()){
//            return;
//        }
//        Long userId = SecurityUtils.getUserId();
//        int dataScope = SecurityUtils.getDataScope();
//        int deptId = SecurityUtils.getDeptId();
//        Set<String> roles = SecurityUtils.getRoles();
//
//        for (String role : roles) {
//            if(role.equals("admin")) {
//                return;
//            }
//        }
//        if(DataScope.OWN.getType()==dataScope) {
//            sql.append(String.format(" or {}.user_id={}", "user", userId));
//        }
    }
}
